<?php
namespace Think\Template\TagLib;
use Think\Template\TagLib;

class Editor extends TagLib
{
    protected $tags = array(
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'editor' => array( 'attr' => 'id,name,style,width,height,type,upload,media', 'close' => 1 )
    );

    /**
     * editor标签解析 插入可视化编辑器
     * 格式： <html:editor id="editor" name="remark" type="all" upload="true" media="true" style="" >{$vo.remark}</html:editor>
     * 
     * @access public
     * @param array $tag
     * @return void
     */
    public function _editor($tag, $content)
    {
        $id     = ! empty($tag['id']) ? $tag['id'] : 'editor';
        $name   = $tag['name'];
        $style  = ! empty($tag['style']) ? $tag['style'] : 'visibility:hidden;';
        $width  = ! empty($tag['width']) ? $tag['width'] : '700px';
        $height = ! empty($tag['height']) ? $tag['height'] : '300px';
        $type   = ! empty($tag['type']) ? $tag['type'] : 'all';
        $upload = ! empty($tag['upload']) ? "true" : "false";
        $media  = ! empty($tag['media']) ? ", 'flash', 'media'" : "";
        switch (strtoupper($type)) {
            case 'basic':
                $items = "['source', '|', 'fullscreen', 'undo', 'redo', 'cut', 'copy', 'paste', '|', 'about']";
                break;
            case 'simple':
                $items = "['source', '|', 'fullscreen', 'undo', 'redo', 'cut', 'copy', 'paste', '|',
                'fontname', 'fontsize', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                'insertunorderedlist', '|', 'emoticons', 'image', 'link', '|', 'about']";
                break;
            default:
                $items = "['source', '|', 'preview', 'fullscreen', 'undo', 'redo', 'print', 'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript', '|', 'selectall', 'clearhtml','quickformat','|',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image'" . $media . ", 'table', 'hr', 'emoticons', 'link', 'unlink', '|', 'about']";
                break;
        }
        // 图片、Flash、视频、文件的本地上传都可开启。默认只有图片，要启用其它的需要修改resource\kindeditor\php下的upload_json.php的相关参数
        $parseStr = '
                <textarea id="' . $id . '" name="' . $name . '" style="width:' . $width . ';height:' . $height . ';' . $style . '">' . $content . '</textarea>
                <script src="__STATIC__/kindeditor/kindeditor-min.js" charset="utf-8"></script>
                <script src="__STATIC__/kindeditor/lang/zh_CN.js" charset="utf-8"></script>
                <script type="text/javascript">
                    var KE;
                    KindEditor.ready(function(K) {
                        KE = K.create("textarea[name=\'' . $name . '\']", {
                                        items : ' . $items . ',
                                        cssPath : "__STATIC__/kindeditor/themes/default/default.css",
                                        allowImageUpload : ' . $upload . ',
                                        allowFlashUpload : false,
                                        allowMediaUpload : false,
                                        allowFileManager : false,
                                        formatUploadUrl : false,
                                        syncType : "form",
                                        uploadJson : "{:U("index/upload")}",
                                        afterCreate : function() {
                                            var self = this;
                                            self.sync();
                                        },
                                        afterChange : function() {
                                            var self = this;
                                            self.sync();
                                        },
                                        afterBlur : function() {
                                            var self = this;
                                            self.sync();
                                        }
                        });
                    KE.appendHtml = function(id,val) {
                                this.html(this.html() + val);
                                if (this.isCreated) {
                                    var cmd = this.cmd;
                                    cmd.range.selectNodeContents(cmd.doc.body).collapse(false);
                                    cmd.select();
                                }
                                return this;
                            }
                    });
                </script>
                    ';
        return $parseStr;
    }
}